/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.service;

import com.google.common.base.Preconditions;
import com.ws.soa.consumer.dao.mapper.UserPaymentFlowMapper;
import com.ws.soa.consumer.dao.mapper.WorksMapper;
import com.ws.soa.consumer.domain.WorksDO;
import com.ws.soa.consumer.dto.WorkInsertDTO;
import com.ws.soa.consumer.util.BoolUtil;
import com.ws.soa.consumer.util.UUIDUtil;
import com.ws.soa.consumer.vo.WorkVO;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

/**
 * WorksService
 *
 * @author luobosi@2dfire.com
 * @since 2019-03-25
 */
@Service
public class WorksService {


  @Resource
  private WorksMapper worksMapper;

  @Resource
  private UserPaymentFlowMapper userPaymentFlowMapper;

  public boolean save(String memberId, WorkInsertDTO workInsertDTO) {
    Preconditions.checkArgument(StringUtils.isNoneEmpty(memberId));
    Preconditions.checkArgument(null != workInsertDTO);

    return worksMapper.insert(this.dtoToDO(memberId, workInsertDTO)) > 0;
  }

  public boolean increaseCommentNum(String id) {
    Preconditions.checkArgument(StringUtils.isNoneEmpty(id));

    return this.worksMapper.increaseCommentNum(id, System.currentTimeMillis()) > 0;
  }

  public boolean increaseLaudNum(String id) {
    Preconditions.checkArgument(StringUtils.isNoneEmpty(id));

    return this.worksMapper.increaseLaudNum(id, System.currentTimeMillis()) > 0;
  }

  public List<WorksDO> findAllByUserId(String userId) {
    Preconditions.checkArgument(StringUtils.isNoneEmpty(userId));

    return this.worksMapper.findAllByUserId(userId);
  }

  /**
   * 获取收费视频列表
   *
   * @return
   */
  public List<WorksDO> findAllTollWorks() {
    return null;
  }

  /**
   * 获取免费视频列表
   *
   * @return
   */
  public List<WorksDO> findAllFreeWords() {
    return null;
  }

  /**
   * 获取某个用户已经购买过的视频
   *
   * @return work
   */
  public List<WorkVO> purchasedWorkList(String userId) {
    List<String> workIds = this.userPaymentFlowMapper.findWorkIdsByUserId(userId);
    List<WorksDO> worksDOS = this.worksMapper.findWorkInWorkIds(workIds);

    return worksDOS.stream().map(worksDO -> this.doToVO(worksDO, workIds))
        .collect(Collectors.toList());
  }

  public List<WorkVO> findAll(String userId) {
    List<String> workIds = this.userPaymentFlowMapper.findWorkIdsByUserId(userId);
    List<WorksDO> worksDOS = this.worksMapper.findAll();

    return worksDOS.stream().map(worksDO -> this.doToVO(worksDO, workIds))
        .collect(Collectors.toList());
  }



  private WorksDO dtoToDO(String memberId, WorkInsertDTO workInsertDTO) {
    WorksDO worksDO = new WorksDO();

    worksDO.setId(UUIDUtil.getUUID());
    worksDO.setUserId(memberId);
    worksDO.setTitle(workInsertDTO.getTitle());
    worksDO.setTitlePage(workInsertDTO.getTitlePage());
    worksDO.setKeywords(workInsertDTO.getKeywords());
    worksDO.setHaveFee(workInsertDTO.getHaveFee());
    worksDO.setChargeAmount(workInsertDTO.getChargeAmount());
    worksDO.setWorkType(workInsertDTO.getWorkType());
    worksDO.setWorkKind(workInsertDTO.getWorkKind());

    return worksDO;
  }

  private WorkVO doToVO(WorksDO worksDO, List<String> sourceWorkIds) {
    WorkVO workVO = new WorkVO();
    workVO.setWorkId(worksDO.getId());
    workVO.setChargeAmount(worksDO.getChargeAmount());
    workVO.setComtNum(worksDO.getComtNum());
    workVO.setHaveFee(worksDO.getHaveFee());
    workVO.setKeywords(worksDO.getKeywords());
    workVO.setLaudNum(worksDO.getLaudNum());
    workVO.setTitle(worksDO.getTitle());
    workVO.setUrl(worksDO.getUrl());
    workVO.setViewNum(worksDO.getViewNum());
    workVO.setCreateTime(worksDO.getCreateTime());
    workVO.setOpTime(worksDO.getOpTime());
    workVO.setHaveViewPermission(BoolUtil.boolToNum(haveViewPermission(worksDO.getId(), sourceWorkIds)));
    workVO.setWorkKind(worksDO.getWorkKind());
    workVO.setWorkType(worksDO.getWorkType());
    return workVO;
  }

  /**
   * 判断给定的 workId 是否有观看权限
   *
   * @param targetWorkId 目标 workId
   * @param sourceWorkIds 已经付费的 workId
   * @return 1-拥有观看权限 0-没有观看权限
   */
  private boolean haveViewPermission(String targetWorkId, List<String> sourceWorkIds) {
    return sourceWorkIds.contains(targetWorkId);
  }
}
